import Api from '../api'

export const state = () => ({
  counter: 0,
  categories: [],
  slides: [],
  keyword: '创业项目关键字',
  menu_state: 'index',
  search: {
    q: '',
    pageSize: 10,
    page: 1,
    c: ''
  },
  searchKeyword: '',
  searchCategory: '',
  conditions: {},
  selectedCondition: {
    industry: '',
    money: '',
    type: '',
    area: '',
    position: '',
    crowd: ''
  },
  industry: ''
})

export const getters = () => {
  return {
    search: state.search
  }
}

export const mutations = {
  increment (state) {
    state.counter++
  },
  setCategories (state, data) {
    state.categories = data
  },
  setSlides (state, data) {
    state.slides = data
  },
  setKeyword (state, key) {
    state.keyword = key
  },
  setMenuState (state, key) {
    state.menu_state = key
  },
  setSearch (state, data) {
    for (const key in data) {
      state.search[key] = data[key]
    }
  },
  setSelectedCondition (state, data) {
    for (const key in data) {
      state.selectedCondition[key] = data[key]
    }
  },
  setSearchKeyword (state, keyword) {
    state.searchKeyword = keyword
  },
  setSearchCategory (state, category) {
    state.searchCategory = category
  },
  setAllConditions (state, conditions) {
    state.conditions = conditions
  },
  setIndustry (state, value) {
    state.industry = value
  }
}

export const actions = {
  /**
   * 获取分类树
   * @param commit
   * @param data
   */
  setCategories ({ commit }, data) {
    Api.getCategoryTree().then((res) => {
      commit('setCategories', res.data.items)
    })
  },
  /**
   * 获取轮播图列表
   * @param commit
   * @param data
   */
  setSlides ({ commit }, data) {
    Api.getSlideList().then((res) => {
      commit('setSlides', res.data.items)
    })
  },
  setItemConditions ({ commit }) {
    Api.getAllConditions().then((res) => {
      // console.log(res)
      commit('setAllConditions', res.data)
    })
  }
}
